package com.mytx.sso.controller;

import com.mytx.common.pojo.CommonResult;
import com.mytx.common.pojo.RedisLock;
import com.mytx.mapper.TopicMapper;
import com.mytx.pojo.Topic;
import com.mytx.common.annotation.SerializedField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;

@RestController
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController.class);


    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/test")
    @SerializedField(encrypt = false)
    public CommonResult test(@RequestParam("user") String user) {

        logger.info("test:" + user + "进来了");
        RedisLock lock = new RedisLock(redisTemplate, "test_lock");
        try {
            if (lock.lock()) {
                logger.info("test:" + user + "获取到了锁");
                Thread.sleep(5000);


                return CommonResult.ok();
            } else {
                logger.info("test:" + user + "没有获取到锁");
                return CommonResult.build(10000, "系统繁忙，没有获取到锁");
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            logger.info("test:" + user + "释放锁");
        }
        return CommonResult.build(10000, "系统繁忙，没有获取到锁");

    }
}
